<?php

namespace admin {

	use C\lib\Db;
	use C\lib\Redis;
    use C\lib\Session;
    use C\mod\worker;
	use C\mod\ads;
	use C\mod\area;
	use C\mod\areaWorker;
	use C\mod\auth;
	use C\mod\workerd;
	use C\lib\Ditu;
	use C\mod\user;


	class auser extends ctr {

        // --- 加载登录页面 ---

		public function main() {
            //Session::start();echo 111;exit;
			$_SESSION['worker.id'] = 1;
			$this->loadView('admin/user/main');

		}



        // --- AJAX ---

		public function login() {

            if($_POST){
                if($ads = ads::get(['uname' => $_POST['user']])) {
                    if($ads->password == md5($_POST['pwd'])) {
                        $_SESSION['worker.id'] = $ads->id;
                        $this->writeJson(1);
                    } else
                        $this->writeJson(0, '账号或密码错误');
                } else
                    $this->writeJson(0, '用户名或密码错误');
            }else{
                $this->main();
            }


		}

		/*
		 * 修改密码
		 */
		public function upPassword(){
		    if($admin=$this->getLogWorker()){
		        if(empty($_POST['password'])){
		            $this->writeJson(0,'请输入新密码!');exit;
                }
                $ads=ads::get(['id'=>$admin->id]);
                $salt = rand(1000, 9999);
		        $ads->set([
                    'paddword'=>md5($_POST['password'] . md5($salt)),
                    'salt'=>$salt,
                    'update_time'=>$_SESSION['REQUEST_TIME']
                ]);
		        if($ads->update()){
		            $this->writeJson(1);
                }else{
		            $this->writeJson(0,'修改失败!');
                }
            }
        }


        /*
         * 退出系统
         */
        public function login_out(){

        }



		/*
		 * 添加员工
		 */
		public function addStaff(){

		   // Session::start();echo 111;exit;
		    if(isset($_SESSION['ads.id'])){
		        if(empty($_POST['realname'])){
		            $this->writeJson(0,'缺少真实姓名!');exit;
                }
                if(empty($_POST['sex'])){
                    $this->writeJson(0,'缺少性别!');exit;
                }
                if(empty($_POST['idCard'])){
                    $this->writeJson(0,'缺少身份证号!');exit;
                }
                if(empty($_POST['department'])){
                    $this->writeJson(0,'缺少部门!');exit;
                }
                if(empty($_POST['position'])){
                    $this->writeJson(0,'缺少职位!');exit;
                }
                if(empty($_POST['addres'])){
                    $this->writeJson(0,'缺少住址!');exit;
                }
                if(empty($_POST['uname'])){
                    $this->writeJson(0,'缺少用户名!');exit;
                }
                if(empty($_POST['password'])){
                    $this->writeJson(0,'缺少密码!');exit;
                }

                $staff=ads::get(['idCard'=>$_POST['idCard']]);
                if($staff){
                    $this->writeJson(0,'此工作人员已存在!');
                }else{
                    $ads=new ads();
                    $salt = rand(1000, 9999);
                    $ads->set([
                        'uname'=>$_POST['uname'],
                        'paddword'=>md5($_POST['password'] . md5($salt)),
                        'realname'=>$_POST['realname'],
                        'salt'=>$salt,
                        'time_add'=>$_SERVER['REQUEST_TIME']
                    ]);
                    if($ads->create()){
                        $this->writeJson(1);
                    }else{
                        $this->writeJson(0,'添加失败!');
                    }
                }
            }else{
		        echo 1;
            }
        }

        /*
         * 员工展示
         */
        private function listStaff($type){
            $_SESSION['ads.id']=1;
            if(isset($_SESSION['ads.id'])){
                //echo 22;exit;
                $where=[];
                $where['type']=$type;
                if(isset($_POST['realname'])){
                    $where['realname']=$_POST['realname'];
                }
                if(isset($_POST['phone'])){
                    $where['phone']=$_POST['phone'];
                }
                if (isset($_POST['lastId']) && trim($_POST['lastId']) != '') {
                   // echo 11;
                    $lastId = trim($_POST['lastId'] + 0);
                } else {
                   // echo 33;exit;
                    $lastId = 0;
                }
                $lastId = ['id','>',$lastId];
//                if(isset($_POST['lastId']) && $_POST['lastId'] == 0) {
//                    $lastId = ['id','>',$lastId];
//                } else {
//
//                }
                $where['lastId']=$lastId;
              // var_dump($where);exit;
                $list=[];

                $data=ads::getList($where,[0,20],['id','DESC']);
               // var_dump($data);exit;
                if($data){
                    $list=$data;
                }
//                $this->loadView("路径",['list'=>$list]);
                return $list;

            }
        }

        /*
         * 技术部
         */

        public function technology(){
          //  echo 55;exit;
            $data=$this->listStaff(1);
            var_dump($data);exit;

            $this->loadView("路径",['list'=>$data]);
        }
        /*
         * 运营部
         */
        public function operate(){
            $data=$this->listStaff(2);
            $this->loadView("路径",['list'=>$data]);
        }

        /*
         * 编辑员工信息
         */
        public function upStaff(){
            Session::start();
            if($_SESSION['ads.id']){
                if(empty($_POST['id'])){
                    $this->writeJson(0,'请选择要编辑的员工!');exit;
                }
                if(empty($_POST['realname'])){
                    $this->writeJson(0,'缺少真实姓名!');exit;
                }
                if(empty($_POST['sex'])){
                    $this->writeJson(0,'缺少性别!');exit;
                }
                if(empty($_POST['idCard'])){
                    $this->writeJson(0,'缺少身份证号!');exit;
                }
                if(empty($_POST['department'])){
                    $this->writeJson(0,'缺少部门!');exit;
                }
                if(empty($_POST['position'])){
                    $this->writeJson(0,'缺少职位!');exit;
                }
                if(empty($_POST['addres'])){
                    $this->writeJson(0,'缺少住址!');exit;
                }
                if(empty($_POST['uname'])){
                    $this->writeJson(0,'缺少用户名!');exit;
                }
                if(empty($_POST['password'])){
                    $this->writeJson(0,'缺少密码!');exit;
                }

                //$staffInfo=ads::get(['id'=>$_POST['id']]);//展示页面写
//                $salt = rand(1000, 9999);
                $ads=ads::get(['id'=>$_POST['id']]);
                $ads->set([
                    'uname'=>$_POST['uname'],
//                    'paddword'=>md5($_POST['password'] . md5($salt)),
                    'realname'=>$_POST['realname'],
//                    'salt'=>$salt,
                    'time_add'=>$_SERVER['REQUEST_TIME']
                ]);
                if($ads->update()){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'修改失败!');
                }
            }
        }
        /*
         * 删除员工
         */
        public function delStaff(){
            Session::start();
            if(isset($_SESSION['ads.id'])){
                if(empty($_POST['ids'])){
                    $this->writeJson(0,'请选择要编辑的员工!');exit;
                }
                foreach ($_POST['ids'] as $val){
                    $delads=ads::delete(['id'=>$val]);
                }
                if($delads){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'删除失败!');
                }
            }
        }

        /************************用户管理*********************************************************/
        /*
         * 快递员管理
         */
        public function listCourier(){
            Session::start();
            if(isset($_SESSION['ads.id'])){
                $where=[];
                //搜索
//                if(isset($_POST['realname'])){
//                    $where['realname']=$_POST['realname'];
//                }
//                if(isset($_POST['phone'])){
//                    $where['phone']=$_POST['phone'];
//                }
                if (isset($_POST['lastId']) && trim($_POST['lastId']) != '') {
                    $lastId = trim($_POST['lastId'] + 0);
                } else {
                    $lastId = 0;
                }
                if($_POST['lastId'] == 0) {
                    $lastId = ['id','>',$lastId];
                } else {
                    $lastId = ['id','<',$lastId];
                }
                $where['lastId']=$lastId;
                $list=[];
                $workers=worker::getList($where,[0,20],['id','DESC']);

                if($workers){
                    foreach ($workers as $val){
                        $wokerd=workerd::get(['worker_id'=>$val->id]);
                        $list[]=[
                            'id'=>$val->id,
                            'realname'=>$wokerd->realname,
                            'phone'=>$wokerd->phone,
                            'addres'=>'将坐标转换为地址',
                            'header_oss'=>$val->head_oss != '' ? STATIC_PATH . $val->head_oss : '',
                            'type'=>$wokerd->type,
                            'subsidiary'=>$wokerd->subsidiary,
                            'adress'=>$wokerd->adress,
                            'verify'=>$wokerd->verify,
                            'contact'=>$wokerd->contact
                        ];
                    }
                }
                $this->loadView("路径",['list'=>$list]);
            }

        }


        /*
         * 快递员添加
         */
        public function addCourier(){
            Session::start();
            if($_SESSION['user.id']){
                if(empty($_POST['realname'])){
                    $this->writeJson(0,'缺少真实姓名!');exit;
                }
                if(empty($_POST['sex'])){
                    $this->writeJson(0,'缺少性别!');exit;
                }
                if(empty($_POST['idCard'])){
                    $this->writeJson(0,'缺少身份证号!');exit;
                }
                if(empty($_POST['department'])){
                    $this->writeJson(0,'缺少部门!');exit;
                }
                if(empty($_POST['position'])){
                    $this->writeJson(0,'缺少职位!');exit;
                }
                if(empty($_POST['addres'])){
                    $this->writeJson(0,'缺少住址!');exit;
                }
                if(empty($_POST['uname'])){
                    $this->writeJson(0,'缺少用户名!');exit;
                }
                if(empty($_POST['password'])){
                    $this->writeJson(0,'缺少密码!');exit;
                }

                $staff=ads::get(['idCard'=>$_POST['idCard']]);
                if($staff){
                    $this->writeJson(0,'此工作人员已存在!');
                }else{
                    $ads=new ads();
//                $auth=new auth();
                    $salt = rand(1000, 9999);
                    $ads->set([
                        'uname'=>$_POST['uname'],
                        'paddword'=>md5($_POST['password'] . md5($salt)),
                        'realname'=>$_POST['realname'],
                        'salt'=>$salt,
                        'time_add'=>$_SERVER['REQUEST_TIME']
                    ]);
                    if($ads->create()){
                        $this->writeJson(1);
                    }else{
                        $this->writeJson(0,'添加失败!');
                    }
                }
            }
        }
        /*
         * 编辑快递员信息
         */
        public function upCourier(){
            Session::start();
            if($_SESSION['ads.id']){
                if(empty($_POST['id'])){
                    $this->writeJson(0,'请选择要编辑的工人!');exit;
                }
                if(empty($_POST['realname'])){
                    $this->writeJson(0,'缺少真实姓名!');exit;
                }
                if(empty($_POST['sex'])){
                    $this->writeJson(0,'缺少性别!');exit;
                }
                if(empty($_POST['idCard'])){
                    $this->writeJson(0,'缺少身份证号!');exit;
                }
                if(empty($_POST['department'])){
                    $this->writeJson(0,'缺少部门!');exit;
                }
                if(empty($_POST['position'])){
                    $this->writeJson(0,'缺少职位!');exit;
                }
                if(empty($_POST['addres'])){
                    $this->writeJson(0,'缺少住址!');exit;
                }
                if(empty($_POST['uname'])){
                    $this->writeJson(0,'缺少用户名!');exit;
                }
                if(empty($_POST['password'])){
                    $this->writeJson(0,'缺少密码!');exit;
                }

                //$staffInfo=ads::get(['id'=>$_POST['id']]);//展示页面写

                $ads=ads::get(['id'=>$_POST['id']]);
                $ads->set([
                    'uname'=>$_POST['uname'],
                    'realname'=>$_POST['realname'],
                    'time_add'=>$_SERVER['REQUEST_TIME']
                ]);
                if($ads->update()){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'修改失败!');
                }
            }
        }
        /*
         * 删除快递员
         */
        public function delCourier(){
            Session::start();
            if(isset($_SESSION['ads.id'])){
                if(empty($_POST['ids'])){
                    $this->writeJson(0,'请选择要删除的快递员!');exit;
                }
                foreach ($_POST['ids'] as $val){
                    $delworker=worker::delete(['id'=>$_POST['id']]);
                    if($delworker){

                        $delworkerd=workerd::delete(['worker_id'=>$_POST['id']]);
                    }

                }
                if($delworkerd){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'删除失败!');
                }
            }
        }


        /*
         * 展示用户信息
         */
        public function listUser(){
            Session::start();
            if(isset($_SESSION['ads.id'])){
                $where=[];
                //搜索
//                if(isset($_POST['realname'])){
//                    $where['realname']=$_POST['realname'];
//                }
//                if(isset($_POST['phone'])){
//                    $where['phone']=$_POST['phone'];
//                }
                if (isset($_POST['lastId']) && trim($_POST['lastId']) != '') {
                    $lastId = trim($_POST['lastId'] + 0);
                } else {
                    $lastId = 0;
                }
                if($_POST['lastId'] == 0) {
                    $lastId = ['id','>',$lastId];
                } else {
                    $lastId = ['id','<',$lastId];
                }
                $where['lastId']=$lastId;
                $list=[];
                $users=user::getList($where,[0,20],['id','DESC']);
                if($users){
                    $list=$users;
                }
                $this->loadView("路径",['list'=>$list]);
            }
        }

        /*
         * 编辑用户信息
         */
        public function upUser(){
            Session::start();
            if($_SESSION['ads.id']){
                if(empty($_POST['id'])){
                    $this->writeJson(0,'请选择要编辑的用户!');exit;
                }
                if(empty($_POST['realname'])){
                    $this->writeJson(0,'缺少真实姓名!');exit;
                }
                if(empty($_POST['sex'])){
                    $this->writeJson(0,'缺少性别!');exit;
                }
                if(empty($_POST['idCard'])){
                    $this->writeJson(0,'缺少身份证号!');exit;
                }
                if(empty($_POST['department'])){
                    $this->writeJson(0,'缺少部门!');exit;
                }
                if(empty($_POST['position'])){
                    $this->writeJson(0,'缺少职位!');exit;
                }
                if(empty($_POST['addres'])){
                    $this->writeJson(0,'缺少住址!');exit;
                }
                if(empty($_POST['uname'])){
                    $this->writeJson(0,'缺少用户名!');exit;
                }
                if(empty($_POST['password'])){
                    $this->writeJson(0,'缺少密码!');exit;
                }

                //$staffInfo=ads::get(['id'=>$_POST['id']]);//展示页面写
                $ads=ads::get(['id'=>$_POST['id']]);
                $ads->set([
                    'uname'=>$_POST['uname'],
                    'realname'=>$_POST['realname'],
                    'time_add'=>$_SERVER['REQUEST_TIME']
                ]);
                if($ads->update()){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'修改失败!');
                }
            }
        }
        /*
         * 删除用户
         */
        public function delUser(){
            Session::start();
            if(isset($_SESSION['ads.id'])){
                if(empty($_POST['ids'])){
                    $this->writeJson(0,'请选择要删除的用户!');exit;
                }
                foreach ($_POST['ids'] as $val){
                    $deluser=user::delete(['id'=>$val]);
                }
                if($deluser){
                    $this->writeJson(1);
                }else{
                    $this->writeJson(0,'删除失败!');
                }
            }
        }




	}

}

